///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_MAT_DETAIL_CPICKSELECTION_H_
#define _BUOLA_MAT_DETAIL_CPICKSELECTION_H_

#include <buola/mat/cexpr.h>

namespace buola { namespace mat { namespace detail {

template<typename tSub,typename tPick>
class CPickSelection
{
    static_assert(QIsReference<tSub>::value,"template argument to pick selection must be a reference");
    static_assert(QIsReference<tPick>::value,"template argument to pick selection must be a reference");
    
public:
    CPickSelection(tSub pSub,tPick pPick)
        :   mSub(pSub)
        ,   mPick(pPick)
    {}

    CPickSelection(const CPickSelection&)=delete;
    CPickSelection& operator=(const CPickSelection&)=delete;

    tSub Sub() const {   return mSub;    }
    
    void operator=(OScalar<tSub> pScalar)
    {
        static_assert(QElementAccess<tSub,ACCESS_ONCE>::value,"can't assign pick selection without element access");
        static_assert(QElementAccess<tPick,ACCESS_ONCE>::value,"can't assign pick selection without element access");
        
        int lRows=mSub.Rows();
        int lCols=mSub.Cols();
        for(int r=0;r<lRows;r++)
        {
            for(int c=0;c<lCols;c++)
            {
                if(mPick.template At<bool>(r,c))
                    mSub.template At<OScalar<tSub>>(r,c)=pScalar;
            }
        }
    }
    
private:
    tSub mSub;
    tPick mPick;
};

/*namespace detail*/ } /*namespace mat*/ } /*namespace buola*/ }

#endif
